/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SystemDataTypes;

import java.sql.ResultSet;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import date.DateParser;

/**
 *
 * @author Administrador
 */

public class FlightPlan {

	public FlightPlan()
	{
	
	}
    public FlightPlan(ResultSet rs) {
        
    //    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy@HH:mm");
        
        try {
            this.aeroline = rs.getString("aeroline");
            this.flightId = rs.getInt("flightId");
            this.cancelled = rs.getBoolean("cancelled");
            this.arrivingDate = DateParser.toDate(rs.getString("arrivingDate"));
            this.leavingDate = DateParser.toDate(rs.getString("leavingDate"));

            JSONArray stops = new JSONArray(rs.getString("stops")); 
            this.stops = new String[stops.length()];
            for(int i=0; i<this.stops.length;i++)
            {
            	this.stops[i] = stops.getString(i);
            }
            
        } catch (Exception ex) {
            Logger.getLogger(FlightPlan.class.getName()).log(Level.SEVERE, null, ex);
        }     
    }

    public FlightPlan(int flightId, String aeroline, Date leavingDate, Date arrivingDate, boolean cancelled) {
        this.flightId = flightId;
        this.aeroline = aeroline;
        this.cancelled = cancelled;
        this.leavingDate = leavingDate;
        this.arrivingDate = arrivingDate;
    }
    
    public int flightId;
    public String aeroline;
    public Date leavingDate;
    public Date arrivingDate;
    public boolean cancelled;
    public String stops[];

    public FlightPlan(JSONObject jSONObject) {
System.out.println(jSONObject.toString());
//        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy@HH:mm");       
        try {

            if (jSONObject.has("flightId"))
                this.flightId = jSONObject.getInt("flightId");

            if (jSONObject.has("cancelled"))
                this.cancelled = jSONObject.getBoolean("cancelled");
            
            if (jSONObject.has("aeroline"))
                this.aeroline = jSONObject.getString("aeroline");
            
            if(jSONObject.has("stops"))
            {
            	JSONArray stops = new JSONArray(jSONObject.getString("stops"));
            	this.stops = new String[stops.length()];
            	for(int i=0; i<stops.length(); i++) this.stops[i] = stops.getString(i);            	
            }

            try {
                
                if (jSONObject.has("leavingDate"))                                    
                    this.leavingDate = DateParser.toDate(jSONObject.getString("leavingDate"));           

                if (jSONObject.has("arrivingDate"))                                    
                    this.arrivingDate = DateParser.toDate(jSONObject.getString("arrivingDate"));           
            
            } catch (Exception ex) {
                Logger.getLogger(FlightPlan.class.getName()).log(Level.SEVERE, null, ex);
                }
            
        } catch (JSONException ex) {
            Logger.getLogger(FlightPlan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
  @Override
  public String toString(){

        JSONObject container = new JSONObject();       
        JSONObject data = new JSONObject();              
       
        //SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy@HH:mm");

        try {
            data.put("flightId", this.flightId);
            data.put("aeroline", this.aeroline);
            data.put("cancelled", this.cancelled);
            data.put("leavingDate", DateParser.toString(this.leavingDate));
            data.put("arrivingDate", DateParser.toString(this.arrivingDate));            
            data.put("stops", (new JSONArray(stops)).toString());
            
            container.put("FlightPlan", data);
        } catch (JSONException ex) {
            Logger.getLogger(FlightPlan.class.getName()).log(Level.SEVERE, null, ex);
        }        
        return container.toString();
  }  
      
}
